import { NestFactory } from '@nestjs/core';
import { Logger } from '@nestjs/common';
import { AppModule } from './app.module';
import { NestExpressApplication } from "@nestjs/platform-express";
import installSwagger from './common/swagger';
import { GlobalHttpFilter } from './common/filters/filters.filter';
import { GlobalInterceptor } from './common/interceptors/interceptors.interceptor';
import { mw as requestIpMw } from 'request-ip'
import { join } from "path";


async function bootstrap() {
  const app = await await NestFactory.create<NestExpressApplication>(AppModule);
  const appPort: number = Number(process.env.APP_PORT) || 3000;

  // 获取请求来源ip
  app.use(requestIpMw({ attributeName: 'ip' }));
  // 规范响应拦截器
  app.useGlobalInterceptors(new GlobalInterceptor());
  // 使用日志服务(在异常拦截里面处理比较好)
  // 注册全局异常过滤
  app.useGlobalFilters(new GlobalHttpFilter());
  // 静态资源托管
  app.useStaticAssets(join(__dirname, '..', 'public'), {
    prefix: '/static',   //设置虚拟路径
  });
  // 开启访问权限控制
  // app.useGlobalGuards(new AuthGuard()) //全局的变量
  // 使用swagger开发文档
  installSwagger(appPort, app);
  await app.listen(appPort, function () {
    console.log(appPort, '后台服务启动成功!')
  });
}
bootstrap();

process.on("beforeExit", () => {
  // console.log('并非这里出错了');
})

process.addListener('uncaughtException', () => {

})

process.addListener('unhandledRejection', () => {

})